﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace FIS.iDET.Keygen
{
    public partial class KeygenForm : Form
    {
        const string notValid = "(Machine ID not valid)";

        public KeygenForm()
        {
            InitializeComponent();
        }

        private void machineIDTextBox_TextChanged(object sender, EventArgs e)
        {
            // check if machine ID is valid
            string machineID = machineIDTextBox.Text.Trim().ToUpper();

            Match match = Regex.Match(
                machineID,
                @"[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{4}"
            );

            if (match.Success)
                serialTextBox.Text = LicenseService.GenarateSerialNumberFrom(machineID);
            else 
                if (serialTextBox.Text != notValid)
                    serialTextBox.Text = notValid;
        }

        private void serialTextBox_Click(object sender, EventArgs e)
        {
            serialTextBox.SelectAll();
        }
    }
}
